<?php
eval('
function xoops_module_uninstall_' . $module_dirname . '(&$module)
{
	return xigg_xoops_module_uninstall("' . $module_dirname . '", $module);
}
');

if (!function_exists('xigg_xoops_module_uninstall')) {
    function xigg_xoops_module_uninstall($module_dirname, &$module)
    {
        // This is a fix for XC Legacy where module data is already removed from the db at this point
        $GLOBALS['xoopsModule'] =& $module;
        require dirname(__FILE__) . '/common.php';
        $uninstaller =& new xigg_xoops_module_uninstaller($xigg);
        if (defined('XOOPS_CUBE_LEGACY')) {
            $root =& XCube_Root::getSingleton();
		    $root->mDelegateManager->add('Legacy.Admin.Event.ModuleUninstall.' . ucfirst($module_dirname) . '.Success', array(&$uninstaller, 'execute'));
		    return true;
        } else {
            require_once 'SabaiXOOPS/XOOPS2ModuleInstallLog.php';
            return $uninstaller->execute($module, new SabaiXOOPS_XOOPS2ModuleInstallLog());
        }
    }

    class xigg_xoops_module_uninstaller
    {
        var $_app;

        function xigg_xoops_module_uninstaller(&$app)
        {
            $this->_app =& $app;
        }

        function execute(&$module, &$log)
        {
            // remove files in cache directories
            $log->add('Removing cache files...');
            $cache_root = $this->_app->getCacheDir();
            foreach (array('HTMLPurifier/HTML', 'HTMLPurifier/CSS', 'HTMLPurifier/URI', 'Cache_Lite') as $dir) {
                if ($dh = opendir($cache_dir = $cache_root . '/' . $dir)) {
                    $log->add(sprintf('Removing cache files under %s.', $cache_dir));
                    while (false !== $file = readdir($dh)) {
                        if (!in_array($file, array('index.html', '.htaccess'))
                              && is_file($cache_file = $cache_dir . '/' . $file)) {
                            if (!@unlink($cache_file)) {
                                $log->addWarning(sprintf('Failed removing cache file %s.', $cache_file));
                            } else {
                                $log->add(sprintf('Removed cache file %s.', $cache_file));
                            }
                        }
                    }
                    closedir($dh);
                }
            }

            // drop database tables
            $log->add('Uninstalling models...');
            $model =& $this->_app->getService('Model');
            foreach (array('Access', 'Category', 'Comment', 'Node', 'Node2tag', 'Plugin', 'Tag', 'Trackback', 'Vote') as $entity_name) {
                $schema =& $model->getSchema($entity_name);
                if (!$schema->drop()) {
                    $log->addWarning(sprintf('Failed deleting database tables for %s.', $entity_name));
                } else {
                    $log->add(sprintf('Database tables for %s dropped.', $entity_name));
                }
                unset($schema);
            }
            return true;
        }
    }
}